package control;

import control.bean.ControlBeanConstant;
import control.bean.ControlPropertiesBean;

import javax.swing.*;
import java.awt.*;

/**
 * Created with IntelliJ IDEA.
 * User: Deshun
 * Date: 2/20/13
 * Time: 9:56 AM
 * To change this template use File | Settings | File Templates.
 */
public class MultiLineTextfield extends Control {

    private int charLimit;

    private JTextArea input;

    public MultiLineTextfield() {
        fieldType = ControlBeanConstant.MULTITEXTFIELD;
        buildControlPanel();
        buildPropertiesPanel();
    }

    @Override
    public Component buildControlPanel() {
        controlPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));
        controlPanel.setOpaque(false);

        label = new JTextField(super.labelText);
        Dimension labelD = new Dimension(130, 20);
        label.setPreferredSize(labelD);
        label.setMinimumSize(labelD);

        input = new JTextArea(5, 20);
        input.setBorder(BorderFactory.createLineBorder(Color.gray, 1));

        controlPanel.addFocusListener(new ControlFocusAdaptor());
        controlPanel.addMouseListener(new ControlMouseAdaptor());

        input.addFocusListener(new ControlFocusAdaptor());
        input.addMouseListener(new ControlMouseAdaptor());
        label.addFocusListener(new ControlFocusAdaptor());
        label.addMouseListener(new ControlMouseAdaptor());

//        controlPanel.setLayout(new GridBagLayout());
//        GridBagConstraints constraints = new GridBagConstraints();
//        constraints.gridx = 0;
//        controlPanel.add(label,constraints);
//        constraints.gridx++;
//        controlPanel.add(input,constraints);
//
        //TODO get the label to align to the top
        controlPanel.add(label);
        controlPanel.add(input);

        return controlPanel;
    }

    private JTextField charMin;
    private JTextField charMax;
    private JCheckBox setMinMax;

    @Override
    public Component buildPropertiesPanel() {
        propertiesPanel = new JPanel(new GridBagLayout());

        fieldProperties = new JLabel("Field Properties");

        controlLabel = new JLabel("Control Name");
        controlName = new JTextField();

        hint = new JCheckBox("Hint");
        required = new JCheckBox("Required");
        readOnly = new JCheckBox("Read Only");
        setMinMax = new JCheckBox("Set Min/Max Characters");

        charMax = new JTextField();
        charMin = new JTextField();

        GridBagConstraints constraints = new GridBagConstraints();
        constraints.gridx = 0;
        constraints.gridy = 0;
        constraints.gridwidth = 2;
//        constraints.ipadx = 130;
        constraints.ipady = 14;
        constraints.anchor = GridBagConstraints.NORTHWEST;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.insets = new Insets(10, 0, 0, 0);
        propertiesPanel.add(fieldProperties, constraints);

        constraints = new GridBagConstraints();
        constraints.gridx = 0;
        constraints.gridy = 1;
        constraints.ipadx = 0;
        constraints.gridwidth = 2;
        constraints.anchor = GridBagConstraints.NORTHWEST;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.insets = new Insets(10, 0, 0, 0);
        propertiesPanel.add(controlLabel, constraints);

        constraints = new GridBagConstraints();
        constraints.gridx = 0;
        constraints.gridy = 2;
        constraints.gridwidth = 2;
        //constraints.gridheight = 2;
//        constraints.ipadx = 114;
        constraints.anchor = GridBagConstraints.NORTHWEST;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.insets = new Insets(5, 0, 0, 0);
        propertiesPanel.add(controlName, constraints);

        constraints = new GridBagConstraints();
        constraints.gridx = 0;
        constraints.gridy = 3;
        constraints.gridwidth = 2;
        constraints.anchor = GridBagConstraints.NORTHWEST;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.insets = new Insets(10, 0, 0, 0);
        propertiesPanel.add(setMinMax, constraints);

        constraints = new GridBagConstraints();
        constraints.gridx = 0;
        constraints.gridy = 4;
        constraints.gridwidth = 2;
        constraints.anchor = GridBagConstraints.NORTHWEST;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.insets = new Insets(1, 0, 0, 0);
        propertiesPanel.add(hint, constraints);

        constraints = new GridBagConstraints();
        constraints.gridx = 0;
        constraints.gridy = 5;
        constraints.gridwidth = 2;
        constraints.anchor = GridBagConstraints.NORTHWEST;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.insets = new Insets(1, 0, 0, 0);
        propertiesPanel.add(required, constraints);

        constraints = new GridBagConstraints();
        constraints.gridx = 0;
        constraints.gridy = 6;
        constraints.gridwidth = 2;
        constraints.anchor = GridBagConstraints.NORTHWEST;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.insets = new Insets(1, 0, 0, 0);
        propertiesPanel.add(readOnly, constraints);

        Component strut = Box.createHorizontalStrut(160);
        constraints.gridy = 6;
        propertiesPanel.add(strut, constraints);

        return propertiesPanel;
    }

    public ControlPropertiesBean exportData() {
        super.exportData();
        try {
            controlPropertiesBean.addProperty(ControlBeanConstant.INPUTVALUE, input.getText());
        } catch ( Exception e ) {
            System.out.println( "No value in multi text field");
        }

        return controlPropertiesBean;
    }

    public void importData(ControlPropertiesBean controlPropertiesBean) {
        try {
            super.importData(controlPropertiesBean);
            input.setText(controlPropertiesBean.getProperty(ControlBeanConstant.INPUTVALUE));
        } catch ( Exception e ) {

        }
    }
}
